package com.sky.wsp.sql.scanner;

import cn.hutool.core.io.FileUtil;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

/**
 * @author wangshaopeng@talkweb.com.cn
 * @desccription 在此输入描述
 * @date 2024/11/12
 */
public class MavenScanner {
    @Test
    public void test() {
        Set<String> log = new HashSet<>(9999);
        File m2 = new File("C:\\.m2\\repository");
        scan(m2, log);
        ArrayList<String> list = new ArrayList<>(log);
        list.sort(String::compareTo);
        FileUtil.writeLines(list, new File("m2_log.txt"), "UTF-8");
    }

    private void scan(File parent, Set<String> log) {
        File[] files = parent.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                scan(file, log);
            }else {
                String name = file.getName();
                if (name.endsWith(".jar")) {
                    name = name.substring(0, name.lastIndexOf('-'));
//                    name = name.substring(0, name.indexOf('-'));
                    if(name.length() < 3){
                        // 太短了容易误伤
                        log.add(name + '-');
                    }else {
                        log.add(name);
                    }
                }
            }
        }
    }
}
